{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "pyttsx3 is a Python library that allows us to create text-to-speech (TTS) applications. \n",
    "It works by using the Microsoft Speech API, which is included in Windows operating systems.\n",
    "'''\n",
    "\n",
    "#coding=utf-8\n",
    "import pyttsx3\n",
    "pyttsx3.speak(\"Hello World!\")\n",
    "pyttsx3.speak(\"前方十米有一个障碍物，请注意向右绕行。\")\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "125\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "import pyttsx3\n",
    "engine = pyttsx3.init() # object creation\n",
    "\n",
    "\"\"\" RATE\"\"\"\n",
    "rate = engine.getProperty('rate')   # getting details of current speaking rate\n",
    "print (rate)                        #printing current voice rate\n",
    "engine.setProperty('rate', 125)     # setting up new voice rate\n",
    "\n",
    "\n",
    "\"\"\"VOLUME\"\"\"\n",
    "volume = engine.getProperty('volume')   #getting to know current volume level (min=0 and max=1)\n",
    "print (volume)                          #printing current volume level\n",
    "engine.setProperty('volume',1.0)    # setting up volume level  between 0 and 1\n",
    "\n",
    "\"\"\"VOICE\"\"\"\n",
    "voices = engine.getProperty('voices')       #getting details of current voice\n",
    "#engine.setProperty('voice', voices[0].id)  #changing index, changes voices. o for male\n",
    "engine.setProperty('voice', voices[0].id)   #changing index, changes voices. 1 for female\n",
    "\n",
    "engine.say(\"Hello World!\")\n",
    "engine.runAndWait()\n",
    "engine.say('My current speaking rate is ' + str(rate))\n",
    "engine.runAndWait()\n",
    "\n",
    "\"\"\"Saving Voice to a file\"\"\"\n",
    "# On linux make sure that 'espeak' and 'ffmpeg' are installed\n",
    "engine.save_to_file('Hello World', 'test.mp3')\n",
    "engine.runAndWait()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这段代码中创建了 pyttsx3 库的 engine 对象的实例。pyttsx3 是一个文本转语音（Text-to-Speech, TTS）的库，允许用户将文本内容转换为语音。以下是对代码中涉及到的方法的介绍及其效果：\n",
    "\n",
    "+ pyttsx3.init()\n",
    "\n",
    "用途：用于创建一个 pyttsx3 引擎的实例，该实例将用于文本转语音的操作。\n",
    "效果：返回一个 engine 对象，通过该对象可以调用其他方法来设置语音属性和合成语音。\n",
    "+ engine.getProperty('rate')\n",
    "\n",
    "用途：获取当前的语音速率（speaking rate），即语音合成的速度。\n",
    "效果：返回一个整型值，表示当前语音的每分钟字数（WPM）。\n",
    "+ engine.setProperty('rate', 125)\n",
    "\n",
    "用途：设置新的语音速率。\n",
    "效果：将语音的速率更改为 125 WPM，使合成的语音变得更慢或更快，具体取决于设置的值。\n",
    "+ engine.getProperty('volume')\n",
    "\n",
    "用途：获取当前的音量级别。\n",
    "效果：返回音量的浮动值，范围在 0.0（最小音量）到 1.0（最大音量）之间。\n",
    "+ engine.setProperty('volume', 1.0)\n",
    "\n",
    "用途：设置新的音量级别。\n",
    "效果：将音量设置为最大值，即 1.0，确保合成的语音音量非常大。\n",
    "+ engine.getProperty('voices')\n",
    "\n",
    "用途：获取系统支持的语音列表。\n",
    "效果：返回一个包含可用语音的列表，每个语音都有自己的属性，如 ID 和名称。\n",
    "+ engine.setProperty('voice', voices[0].id)\n",
    "\n",
    "用途：设置要使用的语音。\n",
    "效果：将语音更改为列表中的第一个语音（通常是男性语音），通过更改索引可以选择不同的语音。\n",
    "+ engine.say(text)\n",
    "\n",
    "用途：将指定的文本添加到语音队列中，准备进行语音合成。\n",
    "效果：在调用 runAndWait() 方法之前，该文本不会被合成出来。\n",
    "+ engine.runAndWait()\n",
    "\n",
    "用途：执行语音合成，处理队列中的所有文本并播放合成的语音。\n",
    "效果：等待语音合成完成后再继续执行后续代码。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cvenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
